package gui.watcher;

import model.Figure;
import model.GameState;
import model.Position;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Ansokol
 * Date: 07.02.12
 * Time: 1:24
 * To change this template use File | Settings | File Templates.
 */
public class GuiWatcher implements Watcher{
    private JFrame window = new JFrame("Tic-Tac-Toe");
    public final JPanel fieldPanel = new JPanel();
    public final JButton [][] field=new JButton[4][4];
    private Map<Figure, ImageIcon> icons;
    private Map<JButton, Figure> buttonFigureMap;
    private JLabel curFig=new JLabel();
    private JPanel upperPanel;
    private JPanel toolPanel;
    private JLabel gameStateLabel;
    private Position currentMove;
    private JButton[] figures;
    public GuiWatcher(){
        icons=new HashMap<Figure,ImageIcon>(16);
        buttonFigureMap=new HashMap<>(16);
        figures = new JButton[16];
        toolPanel=new JPanel();
        toolPanel.setLayout(new GridLayout(2,8));
        for (int i=0; i<16; i++) {
            Figure f= Figure.byCode(i);
            ImageIcon imageIcon= new ImageIcon("images/p"+f.toBinary()+".png");
            figures[i]=new JButton();
            figures[i].setIcon(imageIcon);
            icons.put(f,imageIcon);
            
            buttonFigureMap.put(figures[i], f); 
            toolPanel.add(figures[i]);
            
        }
        upperPanel=new JPanel();
        upperPanel.setLayout(new GridLayout(1,3));
        
        
        window.setSize(700, 700);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new GridLayout(2,1));

        fieldPanel.setLayout(new GridLayout(4,4));
        for (int j=0; j<4; j++) {
            for (int i=0; i<4; i++) {
                field[i][j]=new JButton();
                fieldPanel.add(field[i][j]);
            }
        }
        curFig.setIcon(new ImageIcon("images/empty.png"));
        gameStateLabel =new JLabel();
        gameStateLabel.setOpaque(true);
        gameStateLabel.setText("1");
        upperPanel.add(gameStateLabel);
        upperPanel.add(curFig);
        upperPanel.add(toolPanel);
        window.add(upperPanel);
        window.add(fieldPanel);
        window.setVisible(true);
    }

    @Override
    public Position getCurrentMove() {
        return this.currentMove;
    }

    @Override
    public void watch(Position move, GameState state) {
        currentMove=move;
        curFig.setIcon(icons.get(move.getFigure()));
        for (int j=0; j<4; j++) {
            for (int i=0; i<4; i++) {

                field[i][j].setIcon(icons.get(move.getField().get(i, j)));
                gameStateLabel.setText(state.toString());

                //  fieldButtons[i][j].setRolloverIcon(icons.get(move.getField().get(i, j)));

            }
        }
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

}
